package cn.jiadakeji.parent.common.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.fasterxml.jackson.databind.json.JsonMapper;
import io.swagger.util.Json;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @author hhaoyu
 */
public class AliyunSmsUtils {

    private final static Logger logger = LoggerFactory.getLogger(AliyunSmsUtils.class);

    private final static String endPoint = "ab";
    private final static String accessKeyId ="LTAIM4x5C08DsE37";

    private final static String accessKeySecret = "bAasqmt94LbtE5Qk9ETxFUJmhuehAL";

    private final static String signName = "海码少儿编程";

    private static final String OK = "OK";

    private static final String PRODUCT = "Dysmsapi";

    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    public static final String VerifySmsCode = "SMS_176940642";

    private static final IAcsClient acsClient;

    static {
        IClientProfile profile =  DefaultProfile.getProfile(endPoint,accessKeyId,accessKeySecret);
        DefaultProfile.addEndpoint(endPoint, PRODUCT, DOMAIN);
        acsClient = new DefaultAcsClient(profile);
    }

    public static boolean send(String tempCode, Map<String, String> templateParam , List<String> phones){
        SendSmsRequest request = new SendSmsRequest();
        request.setSysMethod(MethodType.POST);
        request.setPhoneNumbers(StringUtils.join(phones, ","));
        request.setSignName(signName);
        request.setTemplateCode(tempCode);
        request.setTemplateParam(JSON.toJSONString(templateParam));
        try {
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

            if (OK.equals(sendSmsResponse.getCode())) {
                return true;
            }

            logger.debug("send fail[code={}, message={}]", sendSmsResponse.getCode(), sendSmsResponse.getMessage());

        } catch (Exception e) {
            logger.debug(e.getMessage(), e);

        }
        return false;
    }


}
